﻿using System;
using System.Linq;
using System.Threading.Tasks;
using CRM.Extensions;
using CRM.Models;
using Microsoft.Extensions.Primitives;


namespace CRM.Services
{
    //using BuyerDataModel = Models.BuyerData;
    public class BuyerDataService : BaseService
    {
        public Task<DbSetExtension.PaginateStruct<BuyerData>> BuyerIndex()
        {
            // 等等
            Request.TryGetValue("title", out var title);
            Request.TryGetValue("type", out var type);
            int intType = Convert.ToInt32(type);
            string titleStr = Convert.ToString(title);
            return DbContext.BuyersData
                .Where(a => a.State == BuyerData.States.Published)
                .Where(a => titleStr == null || a.Title.Contains(titleStr))
                .Where(a => intType == 0 || a.Type == (BuyerData.Types)intType)
                .OrderByDescending(m => m.Id)
                .ToPaginateAsync(Request);
        }

        public Task<DbSetExtension.PaginateStruct<BuyerData>> UserIndex()
        {
            Request.TryGetValue("title", out var title);
            Request.TryGetValue("type", out var type);
            int intType = Convert.ToInt32(type);
            string titleStr = Convert.ToString(title);
            return DbContext.BuyersData
                .Where(a => titleStr == null || a.Title.Contains(titleStr))
                .Where(a => intType == 0 || a.Type == (BuyerData.Types)intType)
                .Where(a => a.UserId == CurrentUser.Id)
                .Where(a => a.State != BuyerData.States.Deleted)
                .OrderByDescending(m => m.Id)
                .ToPaginateAsync(Request);
        }

        public Task Add(BuyerData vm)
        {
            vm.UserId = CurrentUser.Id;
            vm.GroupId = CurrentUser.GroupId;
            vm.CompanyId = CurrentUser.CompanyId;
            vm.OemId = CurrentUser.OemId;
            DbContext.Add(vm);
            return DbContext.SaveChangesAsync();
        }


        public Task Edit(BuyerData vm)
        {
            DbContext.Update(vm);
            return DbContext.SaveChangesAsync();
        }

        public Task<int> DelInfo(int id)
        {
            var buyerdata = DbContext.BuyersData.FirstOrDefault(a => a.Id == id);
            buyerdata.State = BuyerData.States.Deleted;
            return DbContext.SaveChangesAsync(); ;
        }

        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task<int> Publish(int id)
        {
            var buyerdata = DbContext.BuyersData.FirstOrDefault(a => a.Id == id);
            buyerdata.State = BuyerData.States.Published;
            return DbContext.SaveChangesAsync(); ;
        }

        /// <summary>
        /// 撤销
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task<int> Revoke(int id)
        {
            var buyerdata = DbContext.BuyersData.FirstOrDefault(a => a.Id == id);
            buyerdata.State = BuyerData.States.Revoked;
            return DbContext.SaveChangesAsync(); ;
        }

        // 买家数据拉取日志
        public Task<DbSetExtension.PaginateStruct<BuyerDataPullLog>> BuyerDataLogIndex()
        {
            StringValues etime = string.Empty; 
            return   DbContext.BuyerDataPullLog
                .WhenWhere(Request.TryGetValue("ctime", out var ctime) && Request.TryGetValue("etime", out etime),
                m=>(m.CreateTime>=Convert.ToDateTime(ctime) && m.EndTime<=Convert.ToDateTime(etime) ))
                .OrderByDescending(m => m.Id)
                .ToPaginateAsync(Request);

        }
        // 验证码识别日志
        public Task<DbSetExtension.PaginateStruct<VerifyCodeLog>> VerfityIndex()
        {
            return DbContext.VerifyCodeLog
                .OrderByDescending(m => m.Id)
                .ToPaginateAsync(Request);
        }
    }
}
